package org.spbu.ir.dictionary.diskdic;

import org.spbu.ir.dictionary.Dictionary;
import org.spbu.ir.dictionary.Word;
import org.spbu.ir.dictionary.WordImpl;

import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.MappedByteBuffer;
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.IOException;

import sun.nio.ch.FileChannelImpl;

/**
 * Created by IntelliJ IDEA.
 * User: im203768
 * Date: 02.04.2007
 * Time: 17:41:28
 * To change this template use File | Settings | File Templates.
 */
public class DictionaryImpl implements Dictionary {
    FileChannel channel;
    ABCLinkedList lst;
    private static final long BLOCK_SIZE = 64;
    private ABC abc;

    public DictionaryImpl(File words, File index, ABC abc) throws IOException {
        this.abc = abc;
        lst = new ABCLinkedList(index, abc);
        channel = new RandomAccessFile(words, "rw").getChannel();
    }

    public long getWordId(Word w) throws IOException {
        for(long addr: lst.getPositions(w.getStem().substring(0, abc.getFirstSize()))){
            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, addr * BLOCK_SIZE, BLOCK_SIZE);
            byte len = buffer.get();
            byte[] buff = new byte[len];
            buffer.get(buff);
            String s = new String(buff);
//            System.out.println(s);
            if(s.equals(w.getStem()))
                return addr;
        }
        return -1;
    }

    public long addWord(Word w) throws IOException {
        long id1 = getWordId(w);
        if(id1 >= 0)
            return id1; 
        long position = channel.size();
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, position, BLOCK_SIZE);
        buffer.put((byte) w.getStem().getBytes().length);
        buffer.put(w.getStem().getBytes());
        buffer.force();
        long id = position / BLOCK_SIZE;
        lst.addPossition(w.getStem().substring(0, abc.getFirstSize()), id);
        return id;
    }

    public static void main(String[] args) throws IOException {
        Dictionary d = new DictionaryImpl(new File(".words"), new File(".index"), new EnglishOneLetterABC());
        System.out.println(d.addWord(new WordImpl("Hi")));
        System.out.println(d.addWord(new WordImpl("My")));
        System.out.println(d.addWord(new WordImpl("Ok")));
        System.out.println(d.addWord(new WordImpl("Ok2")));
        System.out.println(d.addWord(new WordImpl("Ok3")));
        System.out.println(d.addWord(new WordImpl("Ok4")));
        System.out.println(d.addWord(new WordImpl("Ok5")));
        System.out.println(d.addWord(new WordImpl("Ok5")));
        System.out.println(d.addWord(new WordImpl("Ok5")));
        System.out.println(d.addWord(new WordImpl("Ok5")));
        System.out.println(d.getWordId(new WordImpl("Ok2")));
        System.out.println(d.getWordId(new WordImpl("My")));
        System.out.println(d.getWordId(new WordImpl("Hi")));


    }
}
